package com.ete.app.main

import android.os.Build
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.ete.app.BR
import com.ete.app.R
import com.ete.app.constant.EteConstant
import com.ete.app.databinding.ActivityMainBinding
import com.ete.lib.upload.UploadConstant
import com.ete.lib.upload.UploadManager
import com.ete.lib.upload.bean.HardwareEntry
import com.ete.lib.upload.hardware.IUploadCallback
import com.ete.lib.upload.unique.UniqueUtils
import com.jkkc.lib.base.core.BaseActivity
import com.jkkc.lib.base.dialog.MiniDialog
import java.lang.reflect.Method

/**
 * Author:LiXian
 * <p>
 * Desc:首页
 * Version:1.0
 * date 2024/1/8 9:54
 */
@Route(path = EteConstant.ROUTER_HOME)
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.activity_main

    override fun initVariableId(): Int = BR.mainVm

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    fun onClick(mView: View) {
        when (mView.id) {
            R.id.main_btn_zip -> {
                uploadLogFile()
            }
            R.id.main_btn_upload -> {
                uploadHardwareInfo()
            }
        }
    }

    private fun uploadLogFile() {
        UploadManager.uploadAppLogFile()
    }

    private fun uploadHardwareInfo() {
        val mEntries = mutableListOf<HardwareEntry>()
        val mEntry = HardwareEntry(
                Build.MODEL.uppercase(),
                UploadConstant.UPLOAD_TYPE_HOST, // 1:主机, 2单机
                DeviceUtils.getUniqueDeviceId(),
                UploadManager.getNetworkType(),
                Build.VERSION.RELEASE,
                AppUtils.getAppVersionName(),
                "",
                TimeUtils.getNowString(),
                getFirmwareVersion(),
                getHardwareVersion(),
                UploadConstant.CONNECT_TYPE_BLE,
                UniqueUtils.getUniqueFlag()
        )
        mEntries.add(mEntry)
        val mDialog = MiniDialog.getMiniDialog(context, false)
        val mCallback = object : IUploadCallback {
            override fun onStart() {
                mDialog.showDialog("正在上报硬件信息")
            }

            override fun onError(msg: String) {
                mDialog.dismissDialog()
                ToastUtils.showShort(msg)
            }

            override fun onFinish() {
                mDialog.dismissDialog()
                ToastUtils.showShort("上报成功")
            }
        }
        UploadManager.uploadHardwareInfo(mEntries, mCallback)
    }

    /**
     * 获取系统固件版本号
     */
    private fun getFirmwareVersion(): String {
        return try {
            val cls = Class.forName("android.os.SystemProperties")
            val method: Method = cls.getMethod("get", String::class.java)
            method.isAccessible = true
            val value = method.invoke(cls, "ro.vendor.sdkversion")
            value?.toString() ?: ""
        } catch (e: java.lang.Exception) {
            ""
        }
    }

    /**
     * 获取主机硬件版本号
     */
    private fun getHardwareVersion(): String {
        return try {
            val cls = Class.forName("android.os.SystemProperties")
            val method: Method = cls.getMethod("get", String::class.java)
            method.isAccessible = true
            val value = method.invoke(cls, "ro.vendor.sdkversion")
            value?.toString() ?: ""
        } catch (e: java.lang.Exception) {
            ""
        }
    }
}